// ===== 引入工具类
const util = require( "./util/util");
const dbUtil = require( "./util/dbUtil");
const apiUtil = require( "./util/apiUtil");
const service = require( "./util/service");
const { selectCount } = require("./util/dbUtil");

exports.testc = async (context) => {
    
    // // 翻牌抽奖 【1111元免单特权】的不抽 --------------- start
    let probability = Math.random();
    let startProbability = 0;
    // 按照中奖率升序排列
    let award_reversal_list = await dbUtil.selectListAll(context,'c_award_reversal_info',{type:{$ne:7},award_number:{$gt:0}},{});
    //筛选
    award_reversal_list = await service.filterForType6Award(context,award_reversal_list);

    let award_reversal = null;
    let endProbability = null;
    for (let awardItem of award_reversal_list) {
        let award_probability = parseFloat(awardItem.award_probability);
        endProbability = startProbability + award_probability;

        if (probability >= startProbability
            && probability < endProbability) {
            award_reversal = awardItem;
            break; 
        }
 
        startProbability=startProbability + award_probability;
    }

    
    return {award_reversal:award_reversal?award_reversal.award_name:"未中奖"
            ,probability:startProbability+"<="+probability+"<"+endProbability
        };
}

/**
 * 收藏/浏览 - 货架列表
 * @param {*} context 默认请求参数上下文
 */
exports.test2c = async (context) => {
    let json = '{"data":[{"index":1,"goods_id":"627949871278","goods_name":"情侣宽松短款羽绒服外套","goods_url":"https://detail.tmall.com/item.htm?id=627949871278"},{"index":2,"goods_id":"627656862446","goods_name":"情侣中长款工装羽绒服","goods_url":"https://detail.tmall.com/item.htm?id=627656862446"},{"index":3,"goods_id":"600512356254","goods_name":"羽绒服爆款加厚韩版防寒","goods_url":"https://detail.tmall.com/item.htm?id=600512356254"},{"index":4,"goods_id":"627036904605","goods_name":"印花立领休闲羽绒服","goods_url":"https://detail.tmall.com/item.htm?id=627036904605"},{"index":5,"goods_id":"602385965181","goods_name":"含绵羊毛休闲毛呢大衣","goods_url":"https://detail.tmall.com/item.htm?id=602385965181"},{"index":6,"goods_id":"626260411216","goods_name":"百搭复古宽松圆领针织衫","goods_url":"https://detail.tmall.com/item.htm?id=626260411216"},{"index":7,"goods_id":"625468773395","goods_name":"卡通狗图案圆领长袖卫衣","goods_url":"https://detail.tmall.com/item.htm?id=625468773395"},{"index":8,"goods_id":"625367032220","goods_name":"休闲通勤高领撞色针织衫","goods_url":"https://detail.tmall.com/item.htm?id=625367032220"},{"index":9,"goods_id":"609187203812","goods_name":"弹力修身直筒休闲牛仔裤","goods_url":"https://detail.tmall.com/item.htm?id=609187203812"},{"index":10,"goods_id":"625228470552","goods_name":"修身小脚运动休闲卫裤","goods_url":"https://detail.tmall.com/item.htm?id=625228470552"},{"index":11,"goods_id":"627674454750","goods_name":"休闲连帽黑色工装羽绒服","goods_url":"https://detail.tmall.com/item.htm?id=627674454750"},{"index":12,"goods_id":"604786084412","goods_name":"加厚羽绒服实用短款工装","goods_url":"https://detail.tmall.com/item.htm?id=604786084412"},{"index":13,"goods_id":"627662138024","goods_name":"羽绒服休闲保暖中长外套","goods_url":"https://detail.tmall.com/item.htm?id=627662138024"},{"index":14,"goods_id":"600513296491","goods_name":"加厚工装中长连帽羽绒服","goods_url":"https://detail.tmall.com/item.htm?id=600513296491"},{"index":15,"goods_id":"606200980823","goods_name":"可拆连帽机车绵羊皮夹克","goods_url":"https://detail.tmall.com/item.htm?id=606200980823"},{"index":16,"goods_id":"625685725587","goods_name":"复古韩系休闲针织毛衫","goods_url":"https://detail.tmall.com/item.htm?id=625685725587"},{"index":17,"goods_id":"627056397385","goods_name":"百搭假两件宽松休闲卫衣","goods_url":"https://detail.tmall.com/item.htm?id=627056397385"},{"index":18,"goods_id":"599610096974","goods_name":"军装风双排扣中长风衣","goods_url":"https://detail.tmall.com/item.htm?id=599610096974"},{"index":19,"goods_id":"598271677666","goods_name":"弹力修身小脚牛仔裤裤子","goods_url":"https://detail.tmall.com/item.htm?id=598271677666"},{"index":20,"goods_id":"599705057508","goods_name":"直筒休闲运动休闲工装裤","goods_url":"https://detail.tmall.com/item.htm?id=599705057508"},{"index":21,"goods_id":"627052852878","goods_name":"时尚休闲连帽工装羽绒服","goods_url":"https://detail.tmall.com/item.htm?id=627052852878"},{"index":22,"goods_id":"610412990513","goods_name":"连帽防泼水运动反光派克","goods_url":"https://detail.tmall.com/item.htm?id=610412990513"},{"index":23,"goods_id":"601885508485","goods_name":"含羊毛翻领 呢子大衣","goods_url":"https://detail.tmall.com/item.htm?id=601885508485"},{"index":24,"goods_id":"602766431494","goods_name":"廓形休闲加厚毛呢大衣","goods_url":"https://detail.tmall.com/item.htm?id=602766431494"},{"index":25,"goods_id":"626261567682","goods_name":"情侣宽松条纹刺绣针织衫","goods_url":"https://detail.tmall.com/item.htm?id=626261567682"},{"index":26,"goods_id":"626261567682","goods_name":"宽松情侣撞色刺绣针织衫","goods_url":"https://detail.tmall.com/item.htm?id=625364724454"},{"index":27,"goods_id":"608329865297","goods_name":"纯色刺绣含羊毛毛呢大衣","goods_url":"https://detail.tmall.com/item.htm?id=608329865297"},{"index":28,"goods_id":"627970167170","goods_name":"基础百搭短款立领夹克","goods_url":"https://detail.tmall.com/item.htm?id=627970167170"},{"index":29,"goods_id":"608853556664","goods_name":"弹力修身显瘦黑色牛仔裤","goods_url":"https://detail.tmall.com/item.htm?id=608853556664"},{"index":30,"goods_id":"611257765449","goods_name":"百搭修身休闲牛仔长裤","goods_url":"https://detail.tmall.com/item.htm?id=611257765449"},{"index":31,"goods_id":"605621548039","goods_name":"可拆卸连帽防泼水棉服","goods_url":"https://detail.tmall.com/item.htm?id=605621548039"},{"index":32,"goods_id":"602356031901","goods_name":"含羊毛棒球服呢子大衣","goods_url":"https://detail.tmall.com/item.htm?id=602356031901"},{"index":33,"goods_id":"624081856728","goods_name":"CLOTTEE联名情侣休闲卫衣","goods_url":"https://detail.tmall.com/item.htm?id=624081856728"},{"index":34,"goods_id":"627677718110","goods_name":"休闲拼色短款立领夹克","goods_url":"https://detail.tmall.com/item.htm?id=627677718110"},{"index":35,"goods_id":"625969795896","goods_name":"基础多色通勤休闲衬衫","goods_url":"https://detail.tmall.com/item.htm?id=625969795896"},{"index":36,"goods_id":"603852849491","goods_name":"含羊毛廓形牛角扣大衣","goods_url":"https://detail.tmall.com/item.htm?id=603852849491"},{"index":37,"goods_id":"600628637536","goods_name":"棒球领个性羽绒服夹克","goods_url":"https://detail.tmall.com/item.htm?id=600628637536"},{"index":38,"goods_id":"627034648867","goods_name":"街头仿羊羔绒牛仔夹克","goods_url":"https://detail.tmall.com/item.htm?id=627034648867"},{"index":39,"goods_id":"612132677609","goods_name":"弹力修身街头休闲牛仔裤","goods_url":"https://detail.tmall.com/item.htm?id=612132677609"},{"index":40,"goods_id":"625972983157","goods_name":"多色休闲锥腿九分裤","goods_url":"https://detail.tmall.com/item.htm?id=625972983157"},{"index":41,"goods_id":"599946020897","goods_name":"羊毛衫日系条纹针织衫","goods_url":"https://detail.tmall.com/item.htm?id=599946020897"},{"index":42,"goods_id":"621645544742","goods_name":"防风双门襟功能性夹克","goods_url":"https://detail.tmall.com/item.htm?id=621645544742"},{"index":43,"goods_id":"627353182796","goods_name":"黑色印花连帽牛仔夹克","goods_url":"https://detail.tmall.com/item.htm?id=627353182796"},{"index":44,"goods_id":"611200492822","goods_name":"帅气工装通勤休闲夹克","goods_url":"https://detail.tmall.com/item.htm?id=611200492822"},{"index":45,"goods_id":"623060701046","goods_name":"宽松多色情侣连帽卫衣","goods_url":"https://detail.tmall.com/item.htm?id=623060701046"},{"index":46,"goods_id":"625972523074","goods_name":"机能风街头加厚工装裤","goods_url":"https://detail.tmall.com/item.htm?id=625972523074"},{"index":47,"goods_id":"625971371683","goods_name":"帅气锥腿运动休闲卫裤","goods_url":"https://detail.tmall.com/item.htm?id=625971371683"},{"index":48,"goods_id":"625393481307","goods_name":"机能风精工刺绣工装裤","goods_url":"https://detail.tmall.com/item.htm?id=625393481307"},{"index":49,"goods_id":"628026842089","goods_name":"宽松做旧微弹休闲牛仔裤","goods_url":"https://detail.tmall.com/item.htm?id=628026842089"},{"index":50,"goods_id":"597476272625","goods_name":"百搭直筒宽松牛仔长裤","goods_url":"https://detail.tmall.com/item.htm?id=597476272625"}]}';
    let json2 = JSON.parse(json);
    console.info(json2.data);
    for (let item of json2.data) {
        await dbUtil.deleteOne(context,'c_collect_goods_info',{goods_id:item.goods_id});
    }
    for (let item of json2.data) {
        await dbUtil.insertOne(context,'c_collect_goods_info',item);
    }
}

/**
 * 明星同款商品信息表
 * @param {*} context 默认请求参数上下文
 */
exports.test3c = async (context) => {
    let json = '{"data":[{"index":1,"goods_id":"628198230899","goods_name":"印花羽绒服","goods_url":"https://detail.tmall.com/item.htm?id=628198230899"},{"index":2,"goods_id":"624917612934","goods_name":"情侣中长款工装羽绒服","goods_url":"https://detail.tmall.com/item.htm?id=624917612934"},{"index":3,"goods_id":"626261567682","goods_name":"情侣宽松撞色条纹刺绣针织衫","goods_url":"https://detail.tmall.com/item.htm?id=626261567682"},{"index":4,"goods_id":"626699908998","goods_name":"复古撞色格纹oversized针织衫","goods_url":"https://detail.tmall.com/item.htm?id=626699908998"},{"index":5,"goods_id":"627410903329","goods_name":"情侣潮酷印花oversized卫衣","goods_url":"https://detail.tmall.com/item.htm?id=627410903329"},{"index":6,"goods_id":"625367536776","goods_name":"个性荧光几何图案针织衣","goods_url":"https://detail.tmall.com/item.htm?id=625367536776"},{"index":7,"goods_id":"627683093533","goods_name":"情侣宽松刺绣潮流连帽卫衣","goods_url":"https://detail.tmall.com/item.htm?id=627683093533"},{"index":8,"goods_id":"626731372800","goods_name":"宽松星期情绪笑脸情侣卫衣","goods_url":"https://detail.tmall.com/item.htm?id=626731372800"},{"index":9,"goods_id":"627036904605","goods_name":"印花立领休闲时尚款羽绒服","goods_url":"https://detail.tmall.com/item.htm?id=627036904605"},{"index":10,"goods_id":"627966743375","goods_name":"保暖连帽款反光羽绒服","goods_url":"https://detail.tmall.com/item.htm?id=627966743375"},{"index":11,"goods_id":"627966727382","goods_name":"工装连帽运动风短款羽绒服","goods_url":"https://detail.tmall.com/item.htm?id=627966727382"},{"index":12,"goods_id":"627056397385","goods_name":"潮流时尚百搭假两件宽松卫衣","goods_url":"https://detail.tmall.com/item.htm?id=627056397385"},{"index":13,"goods_id":"627677718110","goods_name":"休闲拼色短款立领夹克","goods_url":"https://detail.tmall.com/item.htm?id=627677718110"},{"index":14,"goods_id":"627432126549","goods_name":"RADIOWOON联名卡通印花羽绒服","goods_url":"https://detail.tmall.com/item.htm?id=627432126549"},{"index":15,"goods_id":"627135744489","goods_name":"RADIOWOON联名渐变羽绒服","goods_url":"https://detail.tmall.com/item.htm?id=627135744489"},{"index":16,"goods_id":"627618661076","goods_name":"待提供","goods_url":"https://detail.tmall.com/item.htm?id=627618661076"},{"index":17,"goods_id":"待提供","goods_name":"待提供","goods_url":"待提供"},{"index":18,"goods_id":"627656862446","goods_name":"潮流保暖连帽中长款工装羽绒服","goods_url":"https://detail.tmall.com/item.htm?id=627656862446"},{"index":19,"goods_id":"626731372800","goods_name":"宽松星期情绪笑脸情侣卫衣","goods_url":"https://detail.tmall.com/item.htm?id=626731372800"},{"index":20,"goods_id":"625682725565","goods_name":"潮流宽松刺绣运动卫衣","goods_url":"https://detail.tmall.com/item.htm?id=625682725565"},{"index":21,"goods_id":"627572720322","goods_name":"待提供","goods_url":"https://detail.tmall.com/item.htm?id=627572720322"},{"index":22,"goods_id":"待提供","goods_name":"待提供","goods_url":"待提供"}]}'
    let json2 = JSON.parse(json);
    for (let item of json2.data) {
        await dbUtil.deleteOne(context,'c_currency_convert_goods_info',item);
    }
    for (let item of json2.data) {
        await dbUtil.insertOne(context,'c_currency_convert_goods_info',item);
    }
}

/**
 * 礼盒商品信息表(c_gift_goods_info)
 * @param {*} context 默认请求参数上下文
 */
exports.test4c = async (context) => {
    
    let json = ''
    let json2 = JSON.parse(json);
    for (let item of json2.data) {
        await dbUtil.insertOne(context,'c_gift_goods_info',item);
    }
}

// ------------------------------------------------ 

/**
 * 用户中奖列表
 * @param {*} context 默认请求参数上下文
 * @apiParam [award_source] 奖品来源 1
 */
exports.userAwardListc = async (context) => {
    // 参数校验 ---------- start
    let param_data = context.data;
    let param = {
        open_id: context.openId
        // open_id:context.data.open_id,
    }
    let paramRes = await util.paramCheck(param);
    if (paramRes != null) {
        return paramRes;
    }
    // 参数校验 ---------- end
    let list = [];
    if (!param_data.award_source) {// 查询个人全部中奖信息
        list = await dbUtil.selectListAll(context,'c_award_issue',{open_id:param.open_id},{sort:{award_source:-1}});
    }else{// 按来源查询
        list = await dbUtil.selectListAll(context,'c_award_issue',{open_id:param.open_id,award_source:param_data.award_source},{sort:{award_source:-1,award_time:-1}});
    }
    if (list.length >= 1) {
        return{
            code: 0,
            status:0,
            msg: '有数据',
            data:list
        }
    }else{
        return{
            code: 1,
            status:0,
            msg: '没数据'
        }
    }
}

/** 
 * 购买商品获得等额酷币
 * @param {*} context 默认请求参数上下
 * @apiParam nick_name 用户名称补卡
 */
exports.userBuyGoodsGetCurrencyc = async (context) => {
    return {code:-1,msg:"此接口已弃用"};
    // // 参数校验 ---------- start
    // let param = {
    //     open_id: context.openId,
    //     // open_id:context.data.open_id,
    //     nick_name:context.data.nick_name
    // }
    // let paramRes = await util.paramCheck(param);
    // if (paramRes != null) {
    //     return paramRes;
    // }
    // // 参数校验 ---------- end
    
    // let orders_convert_count = await dbUtil.selectCount(context,'c_orders_convert_currency',{open_id:param.open_id});
    // if (orders_convert_count >= 2) {
    //     return {
    //         code:4,
    //         msg:'用户已经购买了2笔订单'
    //     }
    // }
    // let user = await dbUtil.selectOne(context,'c_user',{open_id:param.open_id});
    // let merchant = await dbUtil.selectOne(context,'b_merchant',{open_id:'AAEFunn3AMZds2zGLnjnLmuh'});
    // let trades = await apiUtil.taobaoApiToSoldOrders(context,merchant.access_token,param.open_id,'2020-10-09 00:00:00','2020-11-09 23:59:59','');
    // // console.info(JSON.stringify(trades));
    // if (!trades || trades.total_results <= 0) {
    //     return {
    //         code: 3,// 没有购买过商品
    //         msg: '用户在规定时间内没有购买过商品'
    //     }
    // }

    
    // let currency_convert_goods_list = await dbUtil.selectListAll(context,'c_currency_convert_goods_info',{},{});
    // let trade_array = trades.trades.trade;
    // let payment = 0;
    // for (let i=0; i < trade_array.length; i++) {
    //     let tid = trade_array[i].tid;// 平台订单号
    //     let data = {
    //         open_id:param.open_id,
    //         nick_name:param.nick_name,
    //         tid:tid,
    //         pay_time: trade_array[i].pay_time
    //     }
    //     let orders = trade_array[i].orders.order; // 订单信息
    //     for (let j = 0; j < orders.length; j++) {
    //         for (let z = 0; z < currency_convert_goods_list.length; z++) {
    //             if (Number(currency_convert_goods_list[z].goods_id) == orders[j].num_iid) {
    //                 data.num_iid = orders[j].num_iid;
    //                 data.sku_id = orders[j].sku_id;
    //                 data.title = orders[j].title;
    //                 data.oid = orders[j].oid;
    //                 data.num = orders[j].num;
    //                 data.payment = Number(orders[j].payment);
    //                 payment += Number(orders[j].payment);
    //             }
    //         }
    //     }
    //     if (payment > 0) {
    //         await dbUtil.insertOne(context,'c_orders_convert_currency',data);
    //     }
    // }
    
    // // 增加酷币值
    // if (payment <= 0) {
    //     return{
    //         code:2,
    //         msg:'用户还未购买过指定商品'
    //     }
    // }else {
    //     let user_param = {
    //         game_increase_currency: Number(user.game_increase_currency) + Number(payment.toFixed(0)),
    //         game_current_currency: Number(user.game_current_currency) + Number(payment.toFixed(0)),
    //         update_time:await util.getNowTime()
    //     }
    //     let update = await dbUtil.updateById(context,'c_user',user,user_param);
    //     if (update >= 1) {
    //         return {
    //             code:0,
    //             msg:'购买商品成功增加了['+Number(payment.toFixed(0))+']酷币'
    //         }
    //     }else {
    //         return {
    //             code:1,
    //             msg:'购买商品失败'
    //         }
    //     }
    // }
}

/**
 * 检查是否有新订单，增加抽奖机会（购买top商品/每人上限6次	翻卡次数+3	11.1-11.3  次数+6）
 */
exports.addLotteryNumberIfBoughtTopGoods = async (context) => {
    // 参数校验 ---------- start
    let param = {
        open_id: context.openId,
        // open_id:context.data.open_id,
        nick_name:context.data.nick_name
    }
    let paramRes = await util.paramCheck(param);
    if (paramRes != null) {
        return paramRes;
    }
    // 参数校验 ---------- end
    
    let orders_convert_count = await dbUtil.selectCount(context,'c_user_top_goods_order_log',{open_id:param.open_id});
    if (orders_convert_count >= 6) {
        return {
            code:4,
            msg:'用户已经购买了6笔订单'
        }
    }
    let user = await dbUtil.selectOne(context,'c_user',{open_id:param.open_id});
    let merchant = await dbUtil.selectOne(context,'b_merchant',{open_id:'AAEFunn3AMZds2zGLnjnLmuh'});
    let trades = await apiUtil.taobaoApiToSoldOrders(context,merchant.access_token,param.open_id,'2020-10-09 00:00:00','2020-11-09 23:59:59','');
    // console.info(JSON.stringify(trades));
    if (!trades || trades.total_results <= 0) {
        return {
            code: 3,// 没有购买过商品
            msg: '用户在规定时间内没有购买过商品'
        }
    }

    let currency_convert_goods_list = await dbUtil.selectListAll(context,'c_currency_convert_goods_info',{},{});
    let trade_array = trades.trades.trade;
    //每笔订单应当新增的翻牌次数
    let lottery_numbeForEveryBoughtTopGoods =await service.getLotteryNumberForEveryBoughtTopGoods();
    //总共需要新增的翻牌次数
    let lottery_number = 0;
    //新增的订单数量
    let orderNum = 0;
    for (let i=0; i < trade_array.length; i++) {
        let tid = trade_array[i].tid;// 平台订单号
        let data = {
            open_id:param.open_id,
            nick_name:param.nick_name,
            tid:tid,
            pay_time: trade_array[i].pay_time
        }
        let orders = trade_array[i].orders.order; // 订单信息
        for (let j = 0; j < orders.length; j++) {
            for (let z = 0; z < currency_convert_goods_list.length; z++) {
                if(await service.checkC_user_top_goods_order_logExist(context,orders[j].oid)){
                    //已经保存过，跳过
                    continue;
                }
                if (Number(currency_convert_goods_list[z].goods_id) == orders[j].num_iid) {
                    data.num_iid = orders[j].num_iid;
                    data.sku_id = orders[j].sku_id;
                    data.title = orders[j].title;
                    data.oid = orders[j].oid;
                    data.num = orders[j].num;
                    data.lottery_number =1;
                    lottery_number = lottery_number+lottery_numbeForEveryBoughtTopGoods;
                    data.payment = orders[j].payment;
                    await dbUtil.insertOne(context,'c_user_top_goods_order_log',data);
                    orderNum++;

                    let orders_convert_count = await dbUtil.selectCount(context,'c_user_top_goods_order_log',{open_id:param.open_id});
                    if (orders_convert_count >= 6) {
                       break;
                    }
                }
            }
        }
        
    }
    
    // 增加翻牌次数
    if (lottery_number <= 0) {
        return{
            code:2,
            msg:'用户还未购买过指定商品'
        }
    }else {
        let user_param = {
            lottery_number:user.lottery_number+lottery_number,
            update_time:await util.getNowTime()
        }
        // let update = await dbUtil.updateById(context,'c_user',user,user_param);
        if (update >= 1) {
            return {
                code:0,
                msg:'购买商品成功增加了['+lottery_number+']翻牌机会',
                data:{lottery_number,order_number:orderNum}
            }
        }else {
            return {
                code:1,
                msg:'购买商品失败'
            }
        }
    }
}

/**
 * 明星同款商品货架列表
 * @param {*} context 默认请求参数上下
 */
exports.userBuyGoodsListc = async (context) => {
    let list = await dbUtil.selectListAll(context,'c_currency_convert_goods_info',{},{sort:{index:1}});
    if (list.length >= 1) {
        return{
            code: 0,
            status:0,
            msg: '有数据',
            data:list
        }
    }else{
        return{
            code: 1,
            status:0,
            msg: '没数据'
        }
    }
}

/**
 * 判断是否在活动期间
 * false代表11月10日活动已结束，二楼还在用于公布排行榜单
 */
exports.isDuringInActivity = async (context) => {
    const isDuringInActivity = await util.isBetweenNowTime("2020-10-10 00:00:00",
                                "2020-11-09 23:59:59");
    return {
        code:0,
        status:0,
        msg:isDuringInActivity?"活动期间":"活动结束",
        data:isDuringInActivity
    };
}

/**
 * 翻牌中奖列表-滚动使用
 * @param {*} context 默认请求参数上下
 */
exports.userAwardReversalListc = async (context) => {
    let list = await dbUtil.selectListAll(context,'c_award_issue',{award_source:2,award_type:6},{sort:{award_time:-1},limit:20});
    if (list.length >= 1) {
        return{
            code: 0,
            status:0,
            msg: '有数据',
            data:list
        }
    }else{
        return{
            code: 1,
            status:0,
            msg: '没数据'
        }
    }
}

/**
 * 用户翻牌抽奖
 * @param {*} context 默认请求参数上下
 * @apiParam nick_name 用户昵称明文
 */
exports.userAwardReversalc = async (context) => {
    return {
        code:2,
        status:0,
        msg:'用户没中奖'
    }
    // 参数校验 ---------- start
    let param = {
        open_id: context.openId,
        // open_id: context.data.open_id,
        nick_name:context.data.nick_name
    }
    let paramRes = await util.paramCheck(param);
    if (paramRes != null) {
        return paramRes;
    }
    // 参数校验 ---------- end

    // 判断用户是否还有翻牌次数  ---- start
    let user = await dbUtil.selectOne(context,'c_user',{open_id:param.open_id});
    if (user.lottery_number <= 0) {
        return {
            code:1,
            status:0,
            msg:'用户['+param.nick_name+']没有翻牌机会了'
        }
    }else{
        // 扣减用户翻牌次数
        // await dbUtil.updateById(context,'c_user',user,{lottery_number:(user.lottery_number - 1)});
    }
    // 判断用户是否还有翻牌次数  ---- end

    // // 翻牌抽奖 【1111元免单特权】的不抽 --------------- start
    let probability = Math.random();
    let startProbability = 0;
    // 按照中奖率升序排列
    let award_reversal_list = await dbUtil.selectListAll(context,'c_award_reversal_info',{type:{$ne:7},award_number:{$gt:0}},{});
    //筛选
    award_reversal_list = await service.filterForType6Award(context,award_reversal_list);

    let award_reversal = null;
    let endProbability = null;
    for (let awardItem of award_reversal_list) {
        let award_probability = parseFloat(awardItem.award_probability);
        endProbability = startProbability + award_probability;

        if (probability >= startProbability
            && probability < endProbability) {
            award_reversal = awardItem;
            break; 
        }
 
        startProbability=startProbability + award_probability;
    }
    // 翻牌抽奖 --------------- end


    //保存翻牌日志
    await service.saveAward_reversal_log(context,param.open_id,param.nick_name,award_reversal);

    // 发放奖品就 --------------- start
    if (award_reversal) {// 有值
        // 判断奖品类型 1:谢谢惠顾，2,3,4都为酷币值, 5: 50元优惠券（满51元可用）
        let type = award_reversal.type;
        //减库存
        // award_reversal.award_total_number--;
        award_reversal.award_number--;
        await dbUtil.updateById(context,'c_award_reversal_info',award_reversal,award_reversal);
        
        if (type >= 2 && type <= 5) { // 2,3,4,都为酷币值
            // 增加酷币
            let data = {
                game_increase_currency: (user.game_increase_currency + award_reversal.award_value),
                game_current_currency: (user.game_current_currency + award_reversal.award_value)
            };
            // await dbUtil.updateById(context,'c_user',user,data);        //记录日志
            await service.saveGame_currency_log(context,"翻牌获得积分",award_reversal.award_value);
    
            return {
                code:0,
                status:0,
                msg:'恭喜用户['+param.nick_name+']获得['+award_reversal.award_name+']',
                data:{
                    award_source:award_reversal.source,
                    award_type:award_reversal.type,
                    award_name:award_reversal.award_name,
                    award_number:award_reversal.award_number
                }
            };
        }else if(type == 6){

            //插入记录：中奖记录
            let award_reversal_data = {
                    open_id:param.open_id,
                    nick_name:param.nick_name,
                    award_source:award_reversal.source,
                    award_type:award_reversal.type,
                    award_name:award_reversal.award_name,
                    award_value:award_reversal.award_value,
                    award_img_url:award_reversal.img_url,
                    award_time: await util.getNowTime()
                }
            await dbUtil.insertOne(context,'c_award_issue',award_reversal_data);

            return {
                code:0,
                status:0,
                msg:'恭喜用户['+param.nick_name+']获得['+award_reversal.award_name+']',
                data:{
                    award_source:award_reversal.source,
                    award_type:award_reversal.type,
                    award_name:award_reversal.award_name,
                    award_number:award_reversal.award_number,
                    award_value:award_reversal.award_value
                }
            };
        }
    }

    return {
        code:2,
        status:0,
        msg:'用户['+param.nick_name+']没中奖'
    }
    // 发放奖品就 --------------- end
}

/**
 * 用户今日分享好友列表
 * @param {*} context 默认请求参数上下
 * @apiParam nick_name 用户昵称
 */
exports.shareAppListc = async (context) => {
    // 参数校验 ---------- start
    let param = {
        nick_name:context.data.nick_name
    }
    let paramRes = await util.paramCheck(param);
    if (paramRes != null) {
        return paramRes;
    }
    // 参数校验 ---------- end
    let time_start = await util.getTimeByDay(0);
    let time_end = await util.getTimeByDay(1);
    let share_app_list = await dbUtil.selectListAll(context,'c_share_app',{nick_name:param.nick_name,share_feedback_time:{$gte:time_start,$lt:time_end}},{});
    if (share_app_list.length >= 1) {
        return{
            code: 0,
            status:0,
            msg: '有数据',
            data:share_app_list
        }
    }else{
        return{
            code: 1,
            status:0,
            msg: '没数据'
        }
    }
}

/**
 * 判断用户是否完成当天分享
 * @param {*} context 默认请求参数上下
 * @apiParam nick_name 分享人的用户昵称
 */
exports.shareAppIsCompletec = async (context) => {
    // 参数校验 ---------- start
    let param = {
        nick_name:context.data.nick_name
    }
    let paramRes = await util.paramCheck(param);
    if (paramRes != null) {
        return paramRes;
    }
    // 参数校验 ---------- end
    // 判断用户当天是否完成分享好友的任务 --------- start
    let time_start = await util.getTimeByDay(0);
    let time_end = await util.getTimeByDay(1);
    let share_app_count = await dbUtil.selectCount(context,'c_share_app',{nick_name:param.nick_name,share_feedback_time:{$gte:time_start,$lt:time_end}});
    if (share_app_count >= 30) {
        return {
            code:1,
            status:0,
            msg:'用户['+param.nick_name+']已经完成今日分享好友任务',
            data:parseInt(share_app_count/3)
        }
    }else{
        return {
            code:0,
            status:0,
            msg:'用户['+param.nick_name+']未完成今日分享好友任务',
            data:parseInt(share_app_count/3)
        }
    }
    // 判断用户当天是否完成分享好友的任务 --------- end
}

/**
 * 用户分享好友-增加翻牌次数
 * @param {*} context 默认请求参数上下
 * @apiParam nick_name 分享人的用户昵称
 * @apiParam share_nick_name 被分享用户的名称(明文)
 * @apiParam share_head_image_url 被分享用户的头像
 */
exports.shareAppc = async (context) => {
    // 参数校验 ---------- start
    let param = {
        share_open_id: context.openId,
        share_nick_name:context.data.share_nick_name,
        share_head_image_url:context.data.share_head_image_url,
        nick_name:context.data.nick_name
    }
    let paramRes = await util.paramCheck(param);
    if (paramRes != null) {
        return paramRes;
    }
    // 参数校验 ---------- end

    let user = await dbUtil.selectOne(context,'c_user',{nick_name:param.nick_name});

    // 判断用户当天是否完成分享好友的任务 --------- start
    let time_start = await util.getTimeByDay(0);
    let time_end = await util.getTimeByDay(1);
    let share_app_count = await dbUtil.selectCount(context,'c_share_app',{nick_name:param.nick_name,share_feedback_time:{$gte:time_start,$lt:time_end}});
    if (share_app_count >= 30) {
        return {
            code:2,
            status:0,
            msg:'用户['+param.nick_name+']已经完成今日分享好友任务'
        }
    }
    // 判断用户当天是否完成分享好友的任务 --------- end
    
    // 分享小程序并增加翻牌次数 ---------- start
    // 判断分享过的用户就不能在分享了
    let share_app = await dbUtil.selectOne(context,'c_share_app',{nick_name:param.nick_name,share_open_id:param.share_open_id});
    if (!share_app) {// 没有被分享过
        // 保存分享记录
        param.share_feedback_time = await util.getNowTime();
        let _id = await dbUtil.insertOne(context,'c_share_app',param);
        if (_id) {
            if ((share_app_count + 1)%3==0) {// 完成当天分享好友任务增加翻牌次数
                let lottery_number =3;
                // await dbUtil.updateById(context,'c_user',user,{lottery_number:user.lottery_number+lottery_number});
                return {
                    code:0,
                    status:0,
                    msg:'用户['+param.nick_name+']完成今日分享小程序任务, 并增加['+lottery_number+']次翻牌机会',
                    data:{count:lottery_number}
                }
            }else{
                return {
                    code:1,
                    status:0,
                    msg:'用户['+param.nick_name+']分享['+param.share_nick_name+']成功'
                }
            }
        }
    }else{
        return {
            code:3,
            status:0,
            msg:'用户['+param.share_nick_name+']已经被分享过了'
        }
    }
    // 分享小程序并增加翻牌次数 ---------- end
}

/**
 * 判断用户浏览商品-增加翻牌次数
 * @param {*} context 默认请求参数上下
 * @apiParam nick_name 用户名称
 */
exports.browseGoodsIsCompletec = async (context) => {
    // 参数校验 ---------- start
    let param = {
        open_id: context.openId,
        // open_id: context.data.open_id,
        nick_name:context.data.nick_name
    }
    let paramRes = await util.paramCheck(param);
    if (paramRes != null) {
        return paramRes;
    }
    // 参数校验 ---------- end
    let time_start = await util.getTimeByDay(0);
    let time_end = await util.getTimeByDay(1);
    let goods_count = await dbUtil.selectCount(context,'c_browse_goods',{open_id:param.open_id,browse_number:3,browse_time:{$gte:time_start,$lt:time_end}});
    if (goods_count >= 1) {
        return {
            code:1,
            status:0,
            msg:'用户['+param.open_id+']已经完成今日浏览商品任务',
            data:{
                count:goods_count
            }
        }
    }else{
        let count = 0;
        return {
            code:0,
            status:0,
            msg:'用户['+param.nick_name+']未完成浏览商品任务',
            data:{
                count:count
            }
        }
    }
}
/**
 * 用户浏览商品增加翻牌次数  每天一次,每次3个商品
 * @param {*} context 默认请求参数上下
 * @apiParam nick_name 用户名称
 * @apiParam goods_id 商品id
 * @apiParam goods_name 商品名称
 */
exports.browseGoodsc = async (context) => {
    // 参数校验 ---------- start
    let param = {
        open_id: context.openId,
        // open_id: context.data.open_id,
        nick_name:context.data.nick_name,
        goods_id:context.data.goods_id,
        goods_name:context.data.goods_name
    }
    let paramRes = await util.paramCheck(param);
    if (paramRes != null) {
        return paramRes;
    }

    let user = await dbUtil.selectOne(context,'c_user',{open_id:param.open_id});    
    if(!user){
        return {
            code:-1499,
            status:-1,
            msg:"请先登录"
        };
    }

    // 参数校验 ---------- end
    let lottery_number = 1;
    
    let time_start = await util.getTimeByDay(0);
    let time_end = await util.getTimeByDay(1);
    let goods_count = await dbUtil.selectCount(context,'c_browse_goods',{open_id:param.open_id,browse_number:3,browse_time:{$gte:time_start,$lt:time_end}});
    if (goods_count >= 1) {
        return {
            code:2,
            status:0,
            msg:'用户['+param.open_id+']已经完成今日浏览商品任务'
        };
    }

    // 判断用户是否浏览过当前商品, 浏览过就不记录到库里
    let goods = await dbUtil.selectOne(context,'c_browse_goods',{open_id:param.open_id,goods_id:param.goods_id});
    if (goods) {
        return {
            code:3,
            status:0,
            msg:'用户['+param.nick_name+']已经浏览过['+param.goods_name+']商品了'
        }
    }
    // 获取当天最近一次浏览的信息
    let browse_goods = await dbUtil.selectOne(context,'c_browse_goods',{open_id:param.open_id,browse_time:{$gte:time_start,$lt:time_end}},{sort:{browse_time:-1}});
    if (!browse_goods) { // 第一次浏览商品
        param.browse_number = 1;
        param.browse_time = await util.getNowTime();
        await dbUtil.insertOne(context,'c_browse_goods',param);
        
        // 判断用户是完成当前浏览商品次数的任务
        let time_start = await util.getTimeByDay(0);
        let time_end = await util.getTimeByDay(1);
        let goods_count = await dbUtil.selectCount(context,'c_browse_goods',{open_id:param.open_id,browse_number:1,browse_time:{$gte:time_start,$lt:time_end}});
        if (goods_count >= 3) {
            // 增加翻牌次数
            // await dbUtil.updateById(context,'c_user',user,{lottery_number:(user.lottery_number+lottery_number)});
            return {
                code:0,
                status:0,
                msg:'用户['+param.nick_name+']完成今日浏览商品任务, 并增加['+lottery_number+']次翻牌机会'
            }
        }else{
            return {
                code:1,
                status:0,
                msg:'用户['+param.nick_name+']浏览['+param.goods_name+']商品成功'
            }
        }
    }else{// 非第一次浏览商品
        param.browse_number = browse_goods.browse_number + 1;
        param.browse_time = await util.getNowTime();
        await dbUtil.insertOne(context,'c_browse_goods',param);
        
        let count = await dbUtil.selectCount(context,'c_browse_goods',{
            open_id:param.open_id,
            // browse_number:param.browse_number,
            browse_time:{$gte:time_start,$lt:time_end}});
        if (count >= 3) {
            // 增加翻牌次数
            // let result111 = await dbUtil.updateById(context,'c_user',user,{lottery_number:(user.lottery_number+lottery_number)});
            // console.info("result111==========>",result111)
            // console.info("(user.lottery_number+lottery_number)==========>",(user.lottery_number+lottery_number))
            return {
                code:0,
                status:0,
                msg:'用户['+param.nick_name+']完成今日浏览商品任务, 并增加['+lottery_number+']次翻牌机会'
            }
        }else{
            return {
                code:1,
                status:0,
                msg:'用户['+param.nick_name+']浏览['+param.goods_name+']商品成功'
            }
        }
    }
}

/**
 * 用户收藏商品记录列表
 * @param {*} context 默认请求参数上下
 */
exports.userCollectGoodsListc = async (context) => {
    // 参数校验 ---------- start
    let param = {
        open_id: context.openId
    }
    let paramRes = await util.paramCheck(param);
    if (paramRes != null) {
        return paramRes;
    }
    // 参数校验 ---------- end
    // 按时间升序排列
    let list = await dbUtil.selectListAll(context,'c_collect_goods',{open_id:param.open_id},{sort:{collect_time:1}});
    if (list.length >= 1) {
        return{
            code: 0,
            status:0,
            msg: '有数据',
            data:list
        }
    }else{
        return{
            code: 1,
            status:0,
            msg: '没数据'
        }
    }
}

/**
 * 判断用户是否完成今天的收藏任务
 * @param {*} context 默认请求参数上下
 * @apiParam nick_name 用户名称明文
 */
exports.collectGoodsIsCompletec = async (context) => {
    // 参数校验 ---------- start
    let param = {
        open_id: context.openId,
        nick_name:context.data.nick_name
    }
    let paramRes = await util.paramCheck(param);
    if (paramRes != null) {
        return paramRes;
    }
    // 参数校验 ---------- end

    // 判断用户当天是否完成收藏商品的任务 --------- start
    let time_start = await util.getTimeByDay(0);
    let time_end = await util.getTimeByDay(1);
    let collect_goods_count = await dbUtil.selectCount(context,'c_collect_goods',{open_id:param.open_id,collect_time:{$gte:time_start,$lt:time_end}});
    if (collect_goods_count >= 9) {
        return {
            code:1,
            status:0,
            msg:'用户['+param.nick_name+']已经完成今日收藏商品任务',
            data:parseInt(collect_goods_count/3)
        }
    }else{
        return {
            code:0,
            status:0,
            msg:'用户['+param.nick_name+']当天没有完成收藏任务',
            data:parseInt(collect_goods_count/3)
        }
    }
    // 判断用户当天是否完成收藏商品的任务 --------- end
}

/**
 * 保存用户收藏商品-增加翻牌次数
 * @param {*} context 默认请求参数上下
 * @apiParam nick_name 用户名称明文
 * @apiParam goods_id 商品id
 * @apiParam goods_name 商品名称
 */
exports.collectGoodsc = async (context) => {
    // 参数校验 ---------- start
    let param = {
        open_id: context.openId,
        nick_name:context.data.nick_name,
        goods_id:context.data.goods_id,
        goods_name:context.data.goods_name
    }
    let paramRes = await util.paramCheck(param);
    if (paramRes != null) {
        return paramRes;
    }
    // 参数校验 ---------- end

    let user = await dbUtil.selectOne(context,'c_user',{open_id:param.open_id});

    // 判断用户当天是否完成收藏商品的任务 --------- start
    let time_start = await util.getTimeByDay(0);
    let time_end = await util.getTimeByDay(1);
    let collect_goods_count = await dbUtil.selectCount(context,'c_collect_goods',{open_id:param.open_id,collect_time:{$gte:time_start,$lt:time_end}});
    if (collect_goods_count >= 9) {
        return {
            code:2,
            status:0,
            msg:'用户['+param.nick_name+']已经完成今日收藏商品任务'
        }
    }
    // 判断用户当天是否完成收藏商品的任务 --------- end

    // 收藏商品并增加翻牌次数 ---------- start
    // 判断收藏过的商品就失效了
    let goods = await dbUtil.selectOne(context,'c_collect_goods',{open_id:param.open_id,goods_id:param.goods_id});
    if (!goods) {// 没有收藏过
        // 保存收藏商品信息
        param.collect_time = await util.getNowTime();
        let insert = await dbUtil.insertOne(context,'c_collect_goods',param);
        if (insert) {
            //当前的数量
            const new_collect_goods_count = (collect_goods_count+1);
            if (new_collect_goods_count%3==0) {// 增加翻牌次数
                // 指定时间内翻牌次数不一样 11.4 -11.10
                let lottery_number;
                let boo = await util.isBetweenNowTime('2020-11-01 00:00:00','2020-11-03 23:59:59');
                if (boo) {
                    lottery_number = 3;
                }else{
                    lottery_number = 1;
                }
                // await dbUtil.updateById(context,'c_user',user,{lottery_number:user.lottery_number+lottery_number});
                return {
                    code:1,
                    status:0,
                    msg:'用户['+param.nick_name+']完成今日收藏商品任务, 并增加['+lottery_number+']次翻牌机会',
                    data:lottery_number
                }
            }else{
                return {
                    code:0,
                    status:0,
                    msg:'用户['+param.nick_name+']收藏商品成功'
                }
            }
        }else{
            return {
                code:4,
                status:0,
                msg:'用户['+param.nick_name+']收藏商品失败'
            }
        }
    }else{
        return {
            code:3,
            status:0,
            msg:'用户['+param.nick_name+']已经收藏过['+param.goods_name+']商品了'
        }
    }
    // 收藏商品 ---------- end
}

/**
 * 收藏/浏览商品货架列表
 * @param {*} context 默认请求参数上下
 */
exports.goodsListc = async (context) => {
    // 参数校验 ---------- start
    let param = {
        open_id: context.openId
        // open_id: context.data.open_id
    }
    let paramRes = await util.paramCheck(param);
    if (paramRes != null) {
        return paramRes;
    }
    // 参数校验 ---------- end
    let collect_goods_list = await dbUtil.selectListAll(context,'c_collect_goods_info',{},{sort:{index:1}});

    // 用户收藏商品列表
    let user_collect_goods_list = await dbUtil.selectListAll(context,'c_collect_goods',{open_id:param.open_id},{});

    // 用户浏览商品列表
    let user_browse_goods_list = await dbUtil.selectListAll(context,'c_browse_goods',{open_id:param.open_id},{});

    // 货架列表标识浏览或收藏商品列表 --------- start
    if (user_collect_goods_list.length <= 0){
        for (let i = 0; i < collect_goods_list.length; i++) {
            collect_goods_list[i].is_collect = 0;
        }
    }else{
        for (let i = 0; i < user_collect_goods_list.length; i++) {
            for (let j = 0; j < collect_goods_list.length; j++) {
                if (user_collect_goods_list[i].goods_id == collect_goods_list[j].goods_id) {
                    collect_goods_list[j].is_collect = 1;
                    break;
                }
            }
        }
        
        for (let item of collect_goods_list) {
            if (!item.is_collect) {
                item.is_collect = 0;
            }
        }
    }
    
    if (user_browse_goods_list.length <= 0){
        for (let i = 0; i < collect_goods_list.length; i++) {
            collect_goods_list[i].is_browse = 0;
        }
    }else{
        for (let i = 0; i < user_browse_goods_list.length; i++) {
            for (let j = 0; j < collect_goods_list.length; j++) {
                if (user_browse_goods_list[i].goods_id == collect_goods_list[j].goods_id) {
                    collect_goods_list[j].is_browse = 1;
                    break;
                }
            }
        }
        for (let item of collect_goods_list) {
            if (!item.is_browse) {
                item.is_browse = 0;
            }
        }
    }

    // 货架列表标识浏览或收藏商品列表 --------- end

    if (collect_goods_list.length >= 1) {
        return{
            code: 0,
            status:0,
            msg: '有数据',
            data:collect_goods_list
        }
    }else{
        return{
            code: 1,
            status:0,
            msg: '没数据'
        }
    }
}

/**
 * 酷币兑换奖品信息列表
 * @param {*} context 默认请求参数上下
 */
exports.currencyConvertListc = async (context) => {
    let list = await dbUtil.selectListAll(context,'c_currency_convert_info',{},{sort:{type:1}});
    if (list.length >= 1) {
        let time_start = await util.getTimeByDay(0);
        let time_end = await util.getTimeByDay(1);

        for (let item of list) {//今日限制的总数
            let award_number = await service.getCurrencyConvertDayLimit(item);
            item.award_number=award_number;

            //加乐观锁
            let version = item.version;
            if(!item.version){ item.version=1; }
            item.version++;
            
            // 当天使用酷币兑换的奖品数量
            let count = await dbUtil.selectCount(context,'c_award_issue',{award_source:3,award_type:item.type,award_time:{$gte:time_start,$lt:time_end}});
            item.award_remainingn_number = (item.award_number - count);
            
            if(item.award_total_number<item.award_remainingn_number){
                item.award_remainingn_number=item.award_total_number;
            }
            if(item.award_total_number<=0){
                item.award_number=0;
                item.award_remainingn_number=0;
                award_remainingn_number=0;
            }
            // 修改当天剩余数量
            // list[i-1].award_remainingn_number = (list[i-1].award_number - count)
            let filter = {_id:item._id};
            if(version){
                filter.version=version;
            }
            let updateResult = await dbUtil.update(context,'c_currency_convert_info',filter,item);
             // 扣减奖品数量
            if(updateResult.code!=0){
                console.info("触发乐观锁！！！，不处理update");
            }
        }
        
        return{
            code: 0,
            status:0,
            msg: '有数据',
            data:list
        }
    }else{
        return{
            code: 1,
            status:0,
            msg: '没数据'
        }
    }
}

/**
 * 用户酷币兑换奖品
 * @param {*} context 默认请求参数上下
 * @apiParam nick_name 用户名称 明文
 * @apiParam type 奖品类型 1,2,3,4
 */
exports.userCurrencyConvertc = async (context) => {
    return {
            code:3,
            status:0,
            msg:'活动已结束'
    }
    
    // 参数校验 ---------- start
    let param = {
        open_id: context.openId,
        nick_name:context.data.nick_name,
        type:context.data.type
    }
    let paramRes = await util.paramCheck(param);
    if (paramRes != null) {
        return paramRes;
    }
    // 参数校验 ---------- end

    let currency_convert = await dbUtil.selectOne(context,'c_currency_convert_info',{type:param.type});
    
    // --------------------- 判断用户酷币是否充足 start
    let user = await dbUtil.selectOne(context,'c_user',{open_id:param.open_id});
    if (user.game_current_currency < currency_convert.need_currency) {
        return {
            code:4,
            status:0,
            msg:'用户['+param.nick_name+']的酷币不足'
        }
    }
    // --------------------- 判断用户酷币是否充足 end

    // ------------- 查询用户是否兑换过  start
    let award_issue = await dbUtil.selectOne(context,'c_award_issue',{open_id:param.open_id,award_source:3,award_type:param.type});
    if (award_issue) {
        return {
            code:2,
            status:0,
            msg:'用户['+param.nick_name+']已经兑换过此奖品了'
        }
    }
    // ------------- 查询用户是否兑换过 end

    // ------------- 判断今日奖品数量是否充足 start
    
    //4	999元行李箱	11111	限量3份（指定日期11月9日兑换）
    if(currency_convert.type == 4 && !(await util.isBetweenNowTime("2020-11-09 00:00:00","2020-11-09 23:59:59"))){
    // if(currency_convert.type == 4 && !(await util.isBetweenNowTime("2020-10-28 00:00:00","2020-10-28 23:59:59"))){
        return {
            code:10,
            status:0,
            msg:'该奖品指定日期11月9日兑换哦'
        }
    }
    
    if (currency_convert.award_total_number <= 0) {// 奖品总数不够了
        return {
            code:5,
            status:0,
            msg:'奖品['+currency_convert.award_name+']抽完了'
        }
    }

    // if (currency_convert.award_remainingn_number <=0) {
    //     return {
    //         code:6,
    //         status:0,
    //         msg:'奖品['+currency_convert.award_name+']抽完了'
    //     }
    // }
    
    let checkCurrencyConvertDayLimit = await service.checkCurrencyConvertDayLimit(context,currency_convert);
    if (!checkCurrencyConvertDayLimit) {// 检查每日的数量限制
        return {
            code:3,
            status:0,
            msg:'奖品['+currency_convert.award_name+']今天抽完了'
        }
    }

    
    // ------------- 判断今日奖品数量是否充足 end

    // --------------------- 发放兑换的奖品 start
    let param1 = {
        open_id:param.open_id,
        nick_name:param.nick_name,
        award_source:currency_convert.source,
        award_type:currency_convert.type,
        award_name:currency_convert.award_name,
        award_value:currency_convert.award_value,
        award_img_url:currency_convert.img_url,
        award_time:await util.getNowTime()
    }

    // 记录奖品发放信息
    // let res = await dbUtil.insertOne(context,'c_award_issue',param1);
    // --------------------- 发放兑换的奖品 end
    // if (true) {
        currency_convert.award_total_number = (currency_convert.award_total_number - 1);
        
        let version = currency_convert.version;
        if(!currency_convert.version){ currency_convert.version=1; }
        currency_convert.version++;

        // 扣减奖品数量
        let filter = {_id:currency_convert._id};
        if(version){
            filter.version=version;
        }
        // console.info("filter==========>",filter);
        let updateResult = await dbUtil.update(context,'c_currency_convert_info',filter,currency_convert);
        // console.info("updateResult==========>",updateResult);
        if(updateResult.code!=0){
            return {code:-12,msg:"网络繁忙，请稍后再试"};
        }

        let res = await dbUtil.insertOne(context,'c_award_issue',param1);

        // 扣减用户的酷币值
        // await dbUtil.updateById(context,'c_user',user,{game_current_currency:(user.game_current_currency - currency_convert.need_currency)});
        //记录日志
        await service.saveGame_currency_log(context,"用户酷币兑换奖品-减去",- currency_convert.need_currency);

        param1._id = res;
        return {
            code:0,
            status:0,
            msg:'用户['+param.nick_name+']兑换'+currency_convert.award_name+'成功',
            data:param1
        }
    // }else{
    //     return {
    //         code:1,
    //         status:0,
    //         msg:'用户['+param.nick_name+']兑换'+currency_convert.award_name+'失败'
    //     }
    // }
}


/**
 * 用户预订礼盒兑换酷币
 * @param {*} context 默认请求参数上下
 */
exports.giftGoodsc = async (context) => {
    return {code:-12344,msg:"此接口已弃用"};
    // // 参数校验 ---------- start
    // let param = {
    //     open_id: context.openId
    //     // open_id: context.data.open_id
    // }
    // let paramRes = await util.paramCheck(param);
    // if (paramRes != null) {
    //     return paramRes;
    // }
    // // 参数校验 ---------- end

    // if (!await util.isBetweenNowTime('2020-10-21 00:00:00','2020-10-21 23:59:59')) {
    //     return {
    //         code:4,
    //         msg:'兑换预订礼盒等额酷币不在交易时间范围内'
    //     }
    // }
    
    // let gift_goods_count = await dbUtil.selectCount(context,'c_orders_gift_goods',{open_id:param.open_id});
    // if (gift_goods_count >= 2) {
    //     return {
    //         code:3,
    //         msg:'用户已经购买了2笔订单'
    //     }
    // }

    // let merchant = await dbUtil.selectOne(context,'b_merchant',{open_id:'AAEFunn3AMZds2zGLnjnLmuh'});
    // let trades = await apiUtil.taobaoApiToSoldOrders(context,merchant.access_token,param.open_id,'2020-10-21 00:00:00','2020-10-21 23:59:59','');
    // // console.info(JSON.stringify(trades));
    // if (!trades || trades.total_results <= 0) {
    //     return {
    //         code: 2,// 没有购买过商品
    //         msg: '用户在规定时间内没有购买过商品'
    //     }
    // }

    // // 加载礼盒货架列表
    // let gift_goods_list = await dbUtil.selectListAll(context,'c_gift_goods_info',{},{});
    // let payment = 0;
    // let trade_array = trades.trades.trade;
    // for (let i=0; i < trade_array.length; i++) {
    //     let tid = trade_array[i].tid;// 平台订单号
    //     let data = {
    //         open_id:param.open_id,
    //         nick_name:param.nick_name,
    //         tid:tid, // 平台订单号
    //         pay_time: trade_array[i].pay_time
    //     }

    //     let orders = trade_array[i].orders.order; // 订单信息
    //     for (let j = 0; j < orders.length; j++) {
    //         for (let z = 0; z < gift_goods_list.length; z++) {
    //             if (Number(gift_goods_list[z].goods_id) == orders[j].num_iid) {
    //                 data.num_iid = orders[j].num_iid
    //                 data.title = orders[j].title
    //                 data.oid = orders[j].oid
    //                 data.num = orders[j].num
    //                 data.payment = Number(orders[j].payment)
    //                 payment += Number(orders[j].payment);
    //             }
    //         }
    //     }
    //     if (payment > 0) {
    //         await dbUtil.insertOne(context,'c_orders_gift_goods',data);
    //     }
    // }

    // // 加111个酷币
    // if (payment > 0) {
    //     let user = await dbUtil.selectOne(context,'c_user',{open_id:param.open_id});
    //     await dbUtil.updateById(context,'c_user',user,{
    //         game_increase_currency:(user.game_increase_currency+111),
    //         game_current_currency:(user.game_current_currency + 111)
    //     });
    //     return {
    //         code:0,
    //         msg:'恭喜用户获得[111]个酷币'
    //     }
    // }else{
    //     return{
    //         code:1,
    //         msg:'用户还未购买过商品'
    //     }
    // }
}

/**
 * 预定礼盒货架信息
 * @param {*} context 默认请求参数上下
 */
exports.giftGoodsList = async (context) => {
    let list = await dbUtil.selectListAll(context,'c_gift_goods_info',{},{});
    if (list.length >= 1) {
        return{
            code: 0,
            status:0,
            msg: '有数据',
            data:list
        }
    }else{
        return{
            code: 1,
            status:0,
            msg: '没数据'
        }
    }
}


/**
 * 当前应援解锁信息 -- 未完成
 * @param {*} context 默认请求参数上下
 */
exports.starAidUnlockNowc = async (context) => {
    let now = Date.parse(await util.getNowTime());
    let time_number = Date.parse('2020-10-22 00:00:00');
    // if (now < time_number) {
    //     return{
    //         code:1,
    //         status:0,
    //         msg:'还没到开始时间,开始时间为 2020-10-22 00:00:00'
    //     }
    // }

    // 获取最近没有解锁
    let star_unlock = await dbUtil.selectOne(context,'c_star_unlock_info',{unlock_status:0},{sort:{unlock_time:1}});
    let time = await util.getNowTime();
    // 获取当前时间的日,小时
    // let month = Number(time.substring(5,7));
    // let day = Number(time.substring(8,10));
    // let hour = Number(time.substring(11,13));
    // let minute = Number(time.substring(14,16));
    // let second = Number(time.substring(17,19));
    let current;
    let difference;

    let date = new Date('2020-10-16 00:00:00');
    let num1 = Date.parse(date);// 
    let num2 = Date.parse(time);// 当前时间
    let a = Number((Math.abs(num2 - num1) / 1000 / 60 / 60).toFixed(0));// 小时 35

    if (star_unlock.number >= 1 && star_unlock.number <= 2) {
        // current = Number(star_unlock.need_currency - (star_unlock.need_currency - day - hour - minute - second));// 当前酷币数
        current = Number(star_unlock.need_currency - (star_unlock.need_currency - a*3));// 当前酷币数
        difference = Number(star_unlock.need_currency - current); // 当前酷币数
    }else {
        // current = Number(star_unlock.need_currency - (star_unlock.need_currency - (month + day + hour + minute + second)*2));// 当前酷币数
        current = Number(star_unlock.need_currency - (star_unlock.need_currency - a*10));// 当前酷币数
        difference = Number(star_unlock.need_currency - current); // 当前酷币数
    }

    let info = {
        current:current,
        difference:difference,
        need_currency:star_unlock.need_currency
    }

    return{
        code:0,
        status:0,
        data:info
    }
}

/**
 * 明星应援解锁列表
 * @param {*} context 默认请求参数上下
 */
exports.starAidUnlockListc = async (context) => {
    let list = await dbUtil.selectListAll(context,'c_star_unlock_info',{},{sort:{unlock_time:1}});
    if (list.length >= 1) {
        for (let item  of list) {
            item.unlock_time_stamp = Date.parse(item.unlock_time);
        }
        return{
            code: 0,
            status:0,
            msg: '有数据',
            data:list
        }
    }else{
        return{
            code: 1,
            status:0,
            msg: '没数据'
        }
    }
}

/**
 * 明星权益投币解锁
 * @param {*} context 默认请求参数上下文
 * @apiParam aid_put_currency 应援投酷币数
 * @apiParam nick_name 用户名称(明文)
 */
exports.starAidUnlockc = async (context) => {
    // 参数校验 ---------- start
    let param = {
        open_id: context.openId,
        aid_put_currency:context.data.aid_put_currency,
        nick_name:context.data.nick_name
    }
    let paramRes = await util.paramCheck(param);
    if (paramRes != null) {
        return paramRes;
    }
    // 参数校验 ---------- end

    // 投币
    let user = await dbUtil.selectOne(context,'c_user',{open_id:param.open_id});
    if (user.game_current_currency < param.aid_put_currency) {
        return {
            code:1,
            status:0,
            msg: '用户[' +param.nick_name+ ']酷币不足'
        }
    }else{
        //扣减用户酷币数
        // await dbUtil.updateById(context,'c_user',user,{game_current_currency:(user.game_current_currency-param.aid_put_currency)});

        // 累加 总投币数
        let star_currency = await dbUtil.selectOne(context,'c_star_unlock',{_id:'5f743228964f9719636ce1ca'});
        await dbUtil.updateById(context,'c_star_unlock',star_currency,{aid_put_currency:star_currency.aid_put_currency+param.aid_put_currency});

        // 为距离当前最新的未解锁的内容投币 并判断是否解锁
        let star_unlock = await dbUtil.selectOne(context,'c_star_unlock_info',{unlock_status:0},{sort:{unlock_time:1}});
        let aid_put_currency = star_unlock.aid_put_currency + param.aid_put_currency;
    
        let now_time = Date.parse(await util.getNowTime());
        let unlock_time = Date.parse(star_unlock.unlock_time);
        if (now_time >=  unlock_time) {// 按照时间点开锁
            await dbUtil.updateById(context,'c_star_unlock_info',star_unlock,{aid_put_currency:aid_put_currency,unlock_status:1});
            return{
                code:0,
                status:0,
                msg: '解锁成功',
            }
        }else{
            return{
                code:2,
                status:0,
                msg: '未到解锁时间',
            }
        }
    }
}

/**
 * 保存用户收货地址
 * @param {*} context 默认请求参数上下文
 * @apiParam _id 奖品_id 多个用英文逗号隔开
 * @apiParam telephone 收件人手机号
 * @apiParam address 收件人收货地址
 * @apiParam receive_user_name 收件人姓名
 */
exports.userAddressInfoSavec = async (context) => {
    // 参数校验 ---------- start
    let param = {
        open_id:context.openId,
        _ids:context.data._ids,
        telephone:context.data.telephone,
        address:context.data.address,
        receive_user_name:context.data.receive_user_name
    }
    let paramRes = await util.paramCheck(param);
    if (paramRes != null) {
        return paramRes;
    }
    // 参数校验 ---------- end
    let data = {
        telephone:param.telephone,
        address:param.address,
        receive_user_name:param.receive_user_name
    }
    let _ids = param._ids.split(',');
    for (let id of _ids) {
        await dbUtil.updateById(context,'c_award_issue',{_id:id},data);
    }
    await dbUtil.update(context,'c_user',{open_id:param.open_id},data);
    return {
        code:0,
        status:0,
        msg:'保存收货地址相关信息成功'
    }
}

/**
 * 酷币排行榜列表
 * @param {*} context 默认请求参数上下文
 */
exports.userCurrencyRankingListc = async (context) => {
    let res = await dbUtil.selectListAll(context,'c_user',{},{
        limit: 5,
        sort: { game_increase_currency: -1 }
    });
    if (res.length >= 1) {
        return{
            code: 0,
            status:0,
            msg: '有数据',
            data:res
        }
    }else{
        return{
            code: 1,
            status:0,
            msg: '没数据'
        }
    }
}


/**
 * 我的当前排名(返回当前排名)
 * @param {*} context 默认请求参数上下文
 */
exports.myRanking = async (context) => {
    let user = await dbUtil.selectOne(context,'c_user',{open_id:context.openId});
    if(!user){
        return {
            code:-1499,
            status:-1,
            msg:"请先登录"
        };
    }

    let count = await dbUtil.selectCount(context,'c_user',
                                        {game_increase_currency:{$gt:user.game_increase_currency}});
    return{
                code: 0,
                status:0,
                msg: '有数据',
                data:count+1
            }
}

/**
 * 用户签到天数
 * @param {*} context 默认请求参数上下文
 */
exports.userSignDayc = async (context) => {
    // 参数校验 ---------- start
    let param = {
        // open_id:context.data.open_id,
        open_id: context.openId,
    }
    let paramRes = await util.paramCheck(param);
    if (paramRes != null) {
        return paramRes;
    }
    // 参数校验 ---------- end
    let count = await dbUtil.selectCount(context,'c_user_sign',{open_id:param.open_id},{});
    return{
        code:0,
        data:{
            count:count
        }
    }
}

/**
 * 用户签到列表
 * @param {*} context 默认请求参数上下文
 */
exports.userSignListc = async (context) => {
    // 参数校验 ---------- start
    let param = {
        // open_id:context.data.open_id,
        open_id: context.openId,
    }
    let paramRes = await util.paramCheck(param);
    if (paramRes != null) {
        return paramRes;
    }
    // 参数校验 ---------- end
    let list = await dbUtil.selectListAll(context,'c_user_sign',{open_id:param.open_id},{sort:{user_sign_time:1}});
    if (list.length >= 1) {
        return{
            code: 0,
            status:0,
            msg: '有数据',
            data:list
        }
    }else{
        return{
            code: 1,
            status:0,
            msg: '没数据'
        }
    }
}

/**
 * 用户签到增加酷币数
 * @param {*} context 默认请求参数上下文
 * @apiParam head_image_url 用户头像
 * @apiParam nick_name 用户名称(明文)
 */
exports.userSignSavec = async (context) => {
    // 参数校验 ---------- start
    let param = {
        // open_id:context.data.open_id,
        open_id: context.openId,
        nick_name: context.data.nick_name,
        head_image_url: context.data.head_image_url
    }
    let paramRes = await util.paramCheck(param);
    if (paramRes != null) {
        return paramRes;
    }
    
    let user = await dbUtil.selectOne(context,'c_user',{open_id:param.open_id});

    // -------------- 每天只能打一次卡 start
    let time_start = await util.getTimeByDay(0);// 获取当天开始时间
    let time_end = await util.getTimeByDay(1);// 获取当天结束时间
    // 查询当天是否签到了
    let user_today_sign_count = await dbUtil.selectCount(context,'c_user_sign',{open_id:param.open_id,user_sign_time:{$gte:time_start,$lt:time_end}});
    if (user_today_sign_count >= 1) {
        return{
            code: 1,
            status:0,
            msg: '用户['+param.nick_name+']今天已经签到了'
        }
    }else{
        param.user_sign_time = await util.getNowTime();
        await dbUtil.insertOne(context,'c_user_sign',param);
    }
    // -------------- 每天只能打一次卡 end
    
    // -------------- 打卡获取权益 start
    // 判断是否有达到获得权益的条件
    let user_sign_count = await dbUtil.selectCount(context,'c_user_sign',{open_id:param.open_id});
    if (user_sign_count > 20) {
        return {
            code:2,
            msg:'签到活动已结束'
        }
    }
    let sign_info={};
    console.info("user_sign_count==============>",user_sign_count)
    //签到奖励的积分数
    let award_value =await service.getAward_valueForSign(context,user_sign_count);
    
    // 修改酷币值
    let param1 = {
                game_increase_currency:user.game_increase_currency + Number(award_value),// 维护增加的酷币
                game_current_currency:user.game_current_currency + Number(award_value),// 维护当前的酷币
                update_time:await util.getNowTime()
        };
    // await dbUtil.updateById(context,'c_user',user,param1);
    //保存日志
    await service.saveGame_currency_log(context,"签到获取积分",award_value);

    sign_info.type = 1;//  获得酷币类型 1
    sign_info.value = Number(award_value);
    return {
        code:0,
        status:0,
        msg: '用户['+param.nick_name+']签到成功',
        data:sign_info
    }
}

/**
 * 保存用户关注店铺记录
 * 增加1次翻牌次数
 * @param {*} context 默认请求参数上下文
 * @apiParam head_image_url 用户头像
 * @apiParam nick_name 用户名称明文
 */
exports.userShopAttentionSavec = async (context) => {
    // 参数校验 ---------- start
    let param = {
        open_id: context.openId,
        // open_id:context.data.open_id,
        nick_name: context.data.nick_name,
        head_image_url: context.data.head_image_url
    }
    let paramRes = await util.paramCheck(param);
    if (paramRes != null) {
        return paramRes;
    }
    // 参数校验 ---------- end 

    let user = await dbUtil.selectOne(context,'c_user',{open_id:param.open_id});
    if (!user) {
        return {
            code: -1,
            status:0,
            msg: '用户['+param.nick_name+']不存在'
        }
    }
    if (user.is_shop_attention == 1) {
        return {
            code: 1,
            status:0,
            msg: '用户['+param.nick_name+']已经关注过店铺了'
        }
    }

    param.attention_time = await util.getNowTime();
    // 添加用户关注店铺信息
    await dbUtil.insertOne(context, 'c_user_shop_attention', param);
    // 修改用户翻牌数和是否关注店铺
    // await dbUtil.updateById(context,'c_user',user,{lottery_number:(3 + user.lottery_number),is_shop_attention:1});
    return {
        code: 0,
        status:0,
        msg: '保存用户关注店铺信息-添加成功',
        data: { count: 3 }
    }
}

/**
 * 判断用户是否是会员
 * @param {*} context 默认请求参数上下文
 */
exports.userMenberc = async (context) => {
    // 参数校验 ---------- start
    let param = {
        // open_id:context.data.open_id,
        open_id: context.openId,
        mix_nick: context.mixNick
    }
    let paramRes = await util.paramCheck(param);
    if (paramRes != null) {
        return paramRes;
    }
    // 参数校验 ---------- end
    
    // let user = await dbUtil.selectOne(context,'c_user',{open_id:param.open_id});
    // if(user.is_user_member != 1) {// 如果不是会员
    //     return {
    //         code:0,
    //         status:0,
    //         msg:'用户['+param.open_id+']不是会员'
    //     }
    // }else{
    //     return {
    //         code:1,
    //         status:0,
    //         msg:'用户['+param.open_id+']是会员'
    //     }
    // }
    
    let merchant = await dbUtil.selectOne(context,'b_merchant',{open_id:'AAEFunn3AMZds2zGLnjnLmuh'});
    let isMember = await apiUtil.taobaoApiToMember(context,merchant.access_token,param.mix_nick);
    if(isMember) {// 如果是会员
        return {
            code:1,
            status:0,
            msg:'用户['+param.open_id+']是会员'
        }
    }else{// 如果不是会员
        return {
            code:0,
            status:0,
            msg:'用户['+param.open_id+']不是会员'
        }
    }
}

/**
 * 保存用户入会信息
 * @param {*} context 默认请求参数上下文
 */
exports.userIsMenberc = async (context) => {
    // 参数校验 ---------- start
    let param = {
        // open_id:context.data.open_id,
        open_id: context.openId,
        mix_nick:context.mixNick
    }
    let paramRes = await util.paramCheck(param);
    if (paramRes != null) {
        return paramRes;
    }
    // 参数校验 ---------- end

    let user = await dbUtil.selectOne(context,'c_user',{open_id:param.open_id});
    if(!user){
        return {
            code:-1499,
            status:-1,
            msg:"请先登录"
        };
    }
    
    if (user.is_user_member == 1) {
        return{
            code:3,
            status:0,
            msg:'用户['+param.open_id+']已经入会了'
        }
    }
    // 判断用户是否是会员
    let merchant = await dbUtil.selectOne(context,'b_merchant',{open_id:'AAEFunn3AMZds2zGLnjnLmuh'});
    let isMember = await apiUtil.taobaoApiToMember(context,merchant.access_token,param.mix_nick);
    if(!isMember) {// 如果不是会员
        return {
            code:2,
            status:0,
            msg:'用户['+param.open_id+']不是会员'
        }
    }else{
        let user1 = await dbUtil.selectOne(context,'c_user',{open_id:param.open_id});
        let param1 = {
            is_user_member:1,
            in_member_time:await util.getNowTime(),
            update_time:await util.getNowTime()
        }
        lottery_number = 3;
        param1.lottery_number = user1.lottery_number + lottery_number;// 增加翻牌次数
        // let res = await dbUtil.updateById(context, 'c_user',user1,param1);
        if (res >= 1) {
            return {
                code:0,
                status:0,
                msg:'用户['+param.open_id+']是会员, 并保存会员信息成功',
                data: {lottery_number: lottery_number}
            }
        }else{
            return {
                code:1,
                status:0,
                msg:'用户['+param.open_id+']保存会员信息失败'
            }
        }
    }
}

/**
 * 用户登每天录增加翻牌机会
 * @param {*} context 默认请求参数上下文
 */
exports.userLoginc = async (context) => {
    // 参数校验 ---------- start
    let param = {
        open_id: context.openId,
        nick_name:context.data.nick_name
    }
    let paramRes = await util.paramCheck(param);
    if (paramRes != null) {
        return paramRes; 
    }
    // 参数校验 ---------- end

    let time_start = await util.getTimeByDay(0);// 获取当天开始时间
    let time_end = await util.getTimeByDay(1);// 获取当天结束时间
    // 查询当天是否签到了
    let user_login_count = await dbUtil.selectCount(context,'c_user_login',{open_id:param.open_id});
    if (user_login_count >= 1) {
        return {
            code:1,
            msg:'用户['+param.nick_name+']已经登录过了'
        }
    }
    param.login_time = await util.getNowTime();
    await dbUtil.insertOne(context,'c_user_login',param);

    let user = await dbUtil.selectOne(context,'c_user',{open_id:param.open_id});
    // await dbUtil.updateById(context,'c_user',user,{lottery_number:(user.lottery_number + 3)});
    return {
        code:0,
        msg:'用户['+param.nick_name+']登录成功,并增加3次翻牌机会'
    }
}

/**
 * 查询用户信息
 * @param {*} context 默认请求参数上下文
 */
exports.getUserc = async (context) => {
    // 参数校验 ---------- start
    let param = {
        open_id: context.openId
    }
    let paramRes = await util.paramCheck(param);
    if (paramRes != null) {
        return paramRes; 
    }
    // 参数校验 ---------- end
    let user = await dbUtil.selectOne(context, 'c_user', {open_id:param.open_id});

    if (user) {
        return {
            code:0,
            status:0,
            data: user
        }
    }else{
        return {
            code:1,
            status:0,
            msg:'用户不存在'
        }
    }
}

/**
 * 保存用户信息
 * @param {*} context 默认请求参数上下文
 * @apiParam head_image_url 用户头像
 * @apiParam nick_name 用户名称明文
 */
exports.userSavec = async (context) => {
    // 参数校验 ---------- start
    let param = {
        open_id: context.openId,
        // open_id:context.data.open_id,
        mix_nick: context.mixNick,
        nick_name: context.data.nick_name,
        head_image_url: context.data.head_image_url
    }
    let paramRes = await util.paramCheck(param);
    if (paramRes != null) {
        return paramRes; 
    }
    // 参数校验 ---------- end
    let user = await dbUtil.selectOne(context, 'c_user', {open_id:param.open_id});

    if(!user) { // insert
        param.is_user_member = 0;// 是否是会员
        param.is_shop_attention = 0;// 是否关注过店铺
        param.lottery_number = 0;// 翻卡次数
        param.game_increase_currency = 0;// 用户增加的酷GUY币数
        param.game_current_currency = 0;// 用户当前酷GUY币数
        param.create_time = await util.getNowTime();
        param.update_time = await util.getNowTime();
        let res = await dbUtil.insertOne(context,'c_user',param);
        return {
            code:0,
            status:0,
            msg:'添加成功',
            data: {_id:res}
        }
    }else{
        param.update_time = await util.getNowTime();
        let result = await dbUtil.updateById(context,'c_user',user,param);
        if (result >= 1) {
            return{
                code:0,
                status:0,
                msg:'修改成功,[' + result + ']条数据受影响'
            }
        }else {
            return{
                code:1,
                status:0,
                msg: '修改失败'
            }
        }
    }
}

exports.getList = async (context) => {
    const {tableName} = context.data;
    // c_award_reversal_log
    let pipeline = [
        // {$match:{'award_source':{$eq:3}}},
        {
            $project: {
                // browse_time :{$substr :["$browse_time",0,10]},
                nick_name:'$nick_name'
            }
        },
        {
            $group: { 
            // _id: {award_source:{award_source:'$award_source'},award_type:{award_type:'$award_type'}},
            // _id :{award_source:'$award_source',award_name:'$award_name'},
                _id:{nick_name:'$nick_name'}
            }
        },
    ]

    let list = await dbUtil.aggregate(context,tableName,pipeline);
    let len = list.length;
    // let list2 = [];
    // for (let item of list) {
    //     let data = {
    //         // open_id:item.open_id,
    //         // open_id:item._id.open_id,
    //         browse_time: item._id.browse_time,
    //         open_id: item._id.open_id,
    //         count: item.count
    //     }
    //     list2.push(data);
    // }

    // let pipeline = {
    //      $group: {_id:null,count:{$sum:'$game_current_currency'}}
    // }
    // let list = await dbUtil.aggregate(context,tableName,pipeline);
    // let count = await dbUtil.selectCount(context,tableName,{game_increase_currency:0});
    console.info(context.data.tableName + ' <----------> ' + JSON.stringify(list));
    return {
        list,
        len
    };
}

exports.getCount = async (context) => {
    const {tableName} = context.data;
    const count = await dbUtil.selectCount(context,tableName,{is_user_member:1});
    return count;
}

exports.aggregate = async (context) => {
    const {tableName} = context.data;
    let param = {
        $group:{
            _id: {$dateToString:{format:'%Y-%m-%d',date:new Date('$attention_time')}},
            count: {$sum:1}
        }
    }
    const list = await dbUtil.aggregate(context,tableName,param);
    console.info(context.data.tableName + ' <----------> ' + JSON.stringify(list));
    return list;
}

exports.clearSpaceForC_currency_convert_goods_info = async (context) => {
    const tableName = "c_currency_convert_goods_info";
    const list = await dbUtil.selectListAll(context,tableName,{},{});
    for(let item of list){
        item.goods_id = item.goods_id.trim();
        await dbUtil.updateById(context,tableName,item,item);
    }
    console.info(JSON.stringify(list));
    return list;
}

exports.clearSpaceFor_collect_goods_info = async (context) => {
    const tableName = "c_collect_goods_info";
    const list = await dbUtil.selectListAll(context,tableName,{},{});
    for(let item of list){
        item.goods_id = item.goods_id.trim();
        await dbUtil.updateById(context,tableName,item,item);
    }
    console.info(JSON.stringify(list));
    return list;
}



